<?php
header("Content-type:Content-type: application/json;charset='utf-8'");
$row = file_get_contents('php://input');
$data = json_decode($row);

$email = $data->email;
$name = $data->name;
$arr = array();

if(!$email){
    $arr["code"] = "400";
    $arr["msg"] = "请输入邮箱...";
    $msg = json_encode($arr);
    echo $msg;
    exit;
};
if(!$name){
    $arr["code"] = "400";
    $arr["msg"] = "请输入昵称...";
    $msg = json_encode($arr);
    echo $msg;
    exit;
};

if($email && $name ){
    include "./config.php";
    $sql = "SELECT email FROM subscribe WHERE flg=1 AND email='$email'";
    $sql_1 = "INSERT INTO subscribe(email,name) VALUES('$email','$name')";
    
    $stmt = $conn->query($sql);
    $stmt->execute();
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $result = $stmt->fetchAll();

    if($result){
        $arr["code"] = "400";
        $arr["msg"] = "已经订阅过了哟~";
        $msg = json_encode($arr);
        $conn = null;
        echo $msg;
    }else{
        $stmt = $conn->prepare($sql_1);
        $stmt->execute();
        $result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

        $arr["code"] = "200";
        $arr["msg"] = "订阅成功~";
        $msg = json_encode($arr);
        $conn = null;
        echo $msg;
    };
}else{
    $arr["code"] = "400";
    $arr["msg"] = "数据异常...";
    $msg = json_encode($arr);
    echo $msg;
};
?>